"""
一个很大的2D矩阵，如果某点的值，由它周围某些点的值决定，例如（i，j）点 的值取当前周围8个点的平均值，如何使用MapReduce来实现 20分
"""
nums = [
    [19, 50, 10, 37, 82, 31],
    [25, 61, 39, 88, 43, 57],
    [10, 10, 28, 32, 45, 92],
    [33, 21, 87, 10, 54, 48],
    [10, 10, 44, 66, 83, 32],
    [75, 53, 49, 10, 11, 92]
    ]
############map###################

#仅考虑周围有8个点的值
def my_map(nums):
    #定义结果
    result_path={}
    #搜索集
    each_path={}
    #行数
    row=len(nums)
    #列
    col=len(nums[0])
    for i in range(row):
        for k in range(col):
            each_path[(i+1,k+1)]=nums[i][k] #先记下每个点的当前坐标值
            print(i+1,k+1)
    for j in range(1,row-1):
        for h in range(1,col-1):
            result_path[(j+1,h+1)]=[
                each_path[(j, h)],
                each_path[(j, h + 1)],
                each_path[(j, h + 2)],
                each_path[(j + 1, h)],
                each_path[(j + 1, h + 2)],
                each_path[(j + 2, h)],
                each_path[(j + 2, h + 1)],
                each_path[(j + 2, h + 2)]
            ]
    return result_path

path = my_map(nums)

############reduce###################
def my_reduce(tuple,path):
    sum=0
    for i in path[tuple]:
        sum+=i
    return "{}-->{}".format(tuple,sum/8)

my_reduce(tuple,path)